package org.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * 用户cdc处理程序
 *
 * @author ym
 * @date 2025/06/28 14:15
 */
@Component
@Slf4j
@CanalTable("user_info")
public class UserCdcHandler implements EntryHandler<CDCEntity> {
    private final RedisTemplate<Object, Object> redisTemplate;

    /**
     * 用户cdc处理程序
     *
     * @param redisTemplate redis模板
     */
    public UserCdcHandler(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 更新
     *
     * @param before 以前
     * @param after  之后
     */
    @Override
    public void update(CDCEntity before, CDCEntity after) {
        log.info("before:{}", before);
        log.info("after:{}", after);
        String key = "user:login:" + after.getId();
        redisTemplate.delete(key);
    }

    /**
     * 删除
     *
     * @param cdcEntity 疾控实体
     */
    @Override
    public void delete(CDCEntity cdcEntity) {
        log.info("监听到数据删除,ID:{}", cdcEntity.getId());
        String key = "user:login:" + cdcEntity.getId();
        redisTemplate.delete(key);
    }
}
